const config = require('../../../config')
const util = require("../../../utils/util.js")
Page({
  data: {
    phone: '',
    password: ''
  },

  // 获取输入账号 
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  onLoad: function () {

  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  // 登录 
  login: function (cb) {

    if (this.data.phone.length == 0 || this.data.password.length == 0) {
      wx.showModal({
        title: '提示',
        content: '用户名和密码不能为空',
      })
    } else {
      let that = this;
      util.postData(config.bindUrl, { username: that.data.phone, password: that.data.password, openID: "testopenid" }).then(data => {
        if (data.success) {
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 1000
          })
          wx.request({
            url: config.loginUrl,
            method: "POST",
            data: { __ajax: true, mobileLogin: true, openID: "testopenid" },
            success: res => {
              if (res.data.body) {
                app.globalData.bizUserInfo = res.data.body
                wx.setStorageSync("JSESSIONID", res.data.body.JSESSIONID)
                if(cb)
                 cb()
                wx.navigateBack({

                })
              }
            }
          })
          // wx.request({
          //   url: config.getUserUrl,
          //   data: { openId: getApp().globalData.openid },
          //   success: res => {
          //     getApp().globalData.bizUserInfo = res.data.user;
          //     cb()
          //     wx.navigateBack({

          //     })
          //   }
          // })

        } else {
          // wx.showModal({
          //   title: '绑定失败',
          //   content: '账号密码错误',
          // })
        }
      })
    }
  },
  updateUser: function (res) {
    this.login(function () {
      let userInfo = res.detail.userInfo
      let nickName = userInfo.nickName
      let avatarUrl = userInfo.avatarUrl
      let gender = userInfo.gender //性别 0：未知、1：男、2：女
      let province = userInfo.province
      let city = userInfo.city
      let country = userInfo.country
      let app = getApp()
      if (app.globalData.openid) {
        wx.request({
          url: config.gengXinXiaoChengXuYongHuUrl,
          data: {
            nickName: nickName,
            avatarUrl: userInfo.avatarUrl,
            gender: userInfo.gender, //性别 0：未知、1：男、2：女
            province: userInfo.province,
            city: userInfo.city,
            country: userInfo.country,
            openId: app.globalData.openid,
          },
          success: res => {
            console.log('用户信息更新成功')
          }
        })
      }
    })

  },
  onUnload() {
    wx.reLaunch({
      url: '/pages/demoIndex/index',
    })
  }
}) 